package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BusinessException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.utils.BeanHelper;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetMealDishMapper setmealDishMapper;
    @Autowired
    private SetmealService setmealService;

    //分页查询
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        List<SetmealVO> setmealList = setmealMapper.list(setmealPageQueryDTO);

        Page<SetmealVO> pageResult = (Page<SetmealVO>)setmealList;

        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }


    //新增套餐
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = BeanHelper.copyProperties(setmealDTO, Setmeal.class);
        //设置初始状态
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);

        //保存套餐内的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishes)){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        }

        setmealDishMapper.insertBatch(setmealDishes);
    }

    //批量删除
    @Override
    public void deleteByIds(List<Long> ids) {
        //1. 查询套餐状态，判断是否可以删除
        Long count = setmealMapper.countStatus(ids);
        if(count > 0){
            throw new BusinessException(MessageConstant.SETMEAL_ON_SALE);
        }

        //2.删除套餐时，菜品与套餐关联信息也要删除
        setmealMapper.deleteByid(ids);
        setmealDishMapper.deleteByDishId(ids);
    }

    //根据id查询套餐
    @Override
    public SetmealVO getById(Long id) {
        //1.查询套餐数据
        Setmeal setmeal = setmealMapper.selectById(id);
        //2.查询套餐内的菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        SetmealVO setmealVO = BeanHelper.copyProperties(setmeal, SetmealVO.class);
        if(setmealDishes != null){
            setmealVO.setSetmealDishes(setmealDishes);
        }

        return setmealVO;
    }

    //修改套餐
    @Override
    public void update(SetmealDTO setmealDTO) {
        //1.修改套餐信息
        Setmeal setmeal = BeanHelper.copyProperties(setmealDTO, Setmeal.class);
        setmealMapper.update(setmeal);

        //2.先删除套餐内的菜品数据
        Long id = setmealDTO.getId();
        setmealDishMapper.deleteByDishId(Collections.singletonList(id));

        //3.再添加新的数据进套餐内
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishes)){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
        }
        setmealDishMapper.insertBatch(setmealDishes);


    }

    //批量起售停售
    @Override
    public void startOrStop(Integer status, Long id)
    {
        //1. 如果是 起售 操作 , 判断当前套餐关联的菜品是否有停售的, 如果有, 不允许起售
        if(status == StatusConstant.ENABLE){
            List<Dish> list = setmealMapper.getBySetmealId(id);
            if(!CollectionUtils.isEmpty(list)){
                list.forEach(dish -> {
                    if(dish.getStatus() == StatusConstant.DISABLE){
                        throw new BusinessException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.updateStatus(setmeal);
    }

    //根据分类id查询套餐（返回list）
    @Override
    public List<Setmeal> list(Setmeal setmeal){
        return setmealMapper.listByCategoryId(setmeal);
    }

    //根据套餐id查询包含的菜品列表
    @Override
    public List<DishItemVO> listDish(Long id) {
        return setmealMapper.selectListDish(id);
    }
}
